/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_MODELS_IDX_MAP_MODEL_REMOVE_HPP
#define	_CUBEFS_CORE_MODELS_IDX_MAP_MODEL_REMOVE_HPP

#include <cubefs/core/container/container.hpp>
#include <cubefs/core/btree/pages/tools.hpp>
#include <cubefs/core/models/idx_map/names.hpp>
#include <cubefs/api/models/logs.hpp>

#define CUBEFS_SOURCE "cubefs/core/models/idx_map/model/remove.hpp:" CUBEFS_AT

namespace cubefs    {
namespace ctr       {

using namespace cubefs::btree;

CUBEFS_MODEL_PART_BEGIN(cubefs::models::idx_map::RemoveName)

    typedef typename Base::Types                                                Types;
    typedef typename Base::PageMgr                                              PageMgr;

    typedef typename PageMgr::Page                                              Page;
    typedef typename Page::ID                                                   ID;
    typedef typename PageMgr::Transaction                                       Transaction;

    typedef typename Types::NodeBase                                            NodeBase;
    typedef typename Types::Counters                                            Counters;
    typedef typename Base::Iterator                                             Iterator;

    typedef typename Types::Pages::NodeDispatcher                               NodeDispatcher;
    typedef typename Types::Pages::RootDispatcher                               RootDispatcher;
    typedef typename Types::Pages::LeafDispatcher                               LeafDispatcher;
    typedef typename Types::Pages::NonLeafDispatcher                            NonLeafDispatcher;

    typedef typename Types::Pages::Node2RootMap                                 Node2RootMap;
    typedef typename Types::Pages::Root2NodeMap                                 Root2NodeMap;

    typedef typename Types::Pages::NodeFactory                                  NodeFactory;

    typedef typename Base::Key                                                  Key;
    typedef typename Base::Value                                                Value;

    static const Int Indexes                                                    = Types::Indexes;


    bool RemoveAllEntries(Key from, Key to, Int i, Int type) {
        if (to >= from)
        {
            Iterator ifrom  = type == cubefs::api::IdxMap::LE ? me_.FindLE(from, i, true)  : me_.FindLT(from, i, false);
            Iterator ito    = type == cubefs::api::IdxMap::LE ? me_.FindLE(to, i, true)    : me_.FindLT(to, i, false);

            CUBEFS_TRACE(me_, "RemoveAllEntries", from, to, type, ito.page()->id());

            if (!(ifrom.IsEmpty() || ifrom.IsEof())) {
                me_.RemoveEntries(ifrom, ito);
            }
        }
        return false;
    }


CUBEFS_MODEL_PART_END

}
}

#undef CUBEFS_SOURCE
#endif
